/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package angrypainters.application;

import angrypainters.components.ResourceManager;
import angrypainters.components.ScreensManager;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ParallelTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.animation.Transition;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.util.Duration;

/**
 * FXML Contransroller class
 *
 * @author A7med
 */
public class SplashScreenController implements Initializable {

    private ScreensManager sc;
    private ResourceManager manager;
    @FXML
    HBox container;
    @FXML
    ImageView brush;
    @FXML
    Text t;
    @FXML
    StackPane stack;

    /**
     * Initransializes transhe contransroller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        for (Node n : container.getChildren()) {
            Rectangle r = new Rectangle(n.getLayoutX(), n.getLayoutY(), 200, 650);

            n.setClip(r);
        }

    }

    public ScreensManager getSc() {
        return sc;
    }

    public void setSc(ScreensManager sc) {
        this.sc = sc;

    }

    public ResourceManager getManager() {
        return manager;
    }

    public void setManager(ResourceManager manager) {
        this.manager = manager;
    }

    public void startAnimation() {
        TranslateTransition translate;
        Timeline scale;

        ArrayList<ParallelTransition> parallel = new ArrayList<>();
        for (int i = 0; i < container.getChildren().size(); i++) {
            translate = new TranslateTransition();
            translate.setNode(brush);
            Rectangle n = (Rectangle) container.getChildren().get(i);
            Rectangle clip = (Rectangle) n.getClip();
            scale = new Timeline(new KeyFrame(Duration.millis(1500), new KeyValue(clip.heightProperty(), 0)));
            translate.setFromX(n.getLayoutX());
            translate.setFromY(0);
            translate.setToY(-450);
            translate.setDuration(Duration.millis(1500));

            parallel.add(new ParallelTransition(scale, translate));

        }
        SequentialTransition sequentialTransition = new SequentialTransition();
        sequentialTransition.getChildren().addAll(parallel);
        sequentialTransition.play();
        sequentialTransition.setOnFinished(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                try {
                    Thread.sleep(1000);

                } catch (InterruptedException ex) {
                    Logger.getLogger(SplashScreenController.class.getName()).log(Level.SEVERE, null, ex);
                }
                sc.addScreen("start", manager.getStartScreen());
                sc.addScreen("settings", manager.getSettingsScreen());
                sc.setScreen("settings");
                
            }
        });

    }

}
